﻿@{
    Layout = "~/Views/Shared/_Edit.cshtml";
}
@model Domain.Sys.Role
@section style{
    <link rel="stylesheet" href="~/lib/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css" />
}
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>@Model.Name 的模块权限</legend>
</fieldset>
<form class="layui-form" action="">
    <input type="hidden" name="roleId" value="@Model.Id" />
    <input type="hidden" name="moduleIds" value="@ViewBag.Data" />
    <ul id="treeDemo" class="ztree"></ul>
    <div class="layui-form-item" style="margin-top: 20px;">
        <div class="layui-input-block" style="margin-left:0; text-align:center;">
            <button class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
        </div>
    </div>
</form>
@section script{
    <script src="~/lib/ztree/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="~/lib/ztree/js/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="~/lib/ztree/js/jquery.ztree.excheck.js"></script>
    <script type="text/javascript">
        var setting = {
            check: {
                enable: true
            },
            async: {
                enable: true,
                url: "/Sys/Module/GetJson"
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onAsyncSuccess: initCheckNode,
                onCheck: onCheck
            }
        };

        //选中事件
        function onCheck(e, treeId, treeNode) {
            getCheckedNodeIds();
        }

        //获取所有选中项的ID集合
        function getCheckedNodeIds() {
            var ids = [];
            var CheckedNodes = $.fn.zTree.getZTreeObj("treeDemo").getCheckedNodes();
            if (CheckedNodes.length > 0) {
                for (i = 0; i < CheckedNodes.length; i++) {
                    var id = CheckedNodes[i].id;
                    if (id != "")
                        ids.push(id);
                }
            }
            $("[name=moduleIds]").val(ids.join(','));
        }

        //过滤条件
        function filter(node) {
            var selectNodes = $("[name=moduleIds]").val().split(",");
            return $.inArray(node.id.toString(), selectNodes) != -1;
        }

        //初始化选中节点
        function initCheckNode() {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
                nodes = zTree.getNodesByFilter(filter);//节点集合
            for (var i = 0; i < nodes.length; i++) {
                zTree.checkNode(nodes[i], true, false);
            }
        }

        $(function () {
            $.fn.zTree.init($("#treeDemo"), setting);
        })

        layui.use("form", function () {
            var form = layui.form
                , layer = layui.layer
                , $ = layui.$;


            var index = parent.layer.getFrameIndex(window.name);

            //监听提交
            form.on('submit(submit)', function (data) {
                var formData = data.field;
                $.post("ModuleSave", formData, function (d) {
                    console.log(d);
                    if (d.code == 0) {
                        layer.msg(d.msg, { icon: 1 });
                        setTimeout(function () {
                            parent.layer.close(index);
                        }, 1000);
                    }
                    else {
                        layer.msg(d.msg, { icon: 2 });
                    }
                })
                return false;
            });
        });
    </script>
}